package com.changan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changan.entity.LoginLog;
import com.changan.entity.User;
import com.changan.exception.BusinessException;
import com.changan.response.Result;
import com.changan.response.ResultCode;
import com.changan.service.LoginLogService;
import com.changan.service.UserService;
import com.changan.vo.UserVO;
import com.wf.captcha.utils.CaptchaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author NieChangan
 * @since 2022-03-02
 */
@Api(tags = "用户管理")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginLogService loginLogService;

    @ApiOperation(value = "获取用户列表")
    @GetMapping("/getUsers")
    public Result getUsers(){
        int i = 1/0;
        List<User> users = userService.list(null);
        return Result.ok().data("users",users);
    }

    @ApiOperation(value = "通过id获取用户信息")
    @GetMapping("/getUserById")
    public Result getUserById(Long id){
        User user = userService.getById(id);
        if(user == null){
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("user",user);
    }

    /**
     * 用户列表
     *
     * @return
     */
    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @PostMapping("/findUserList")
    public Result findUserList(@RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
                               @RequestParam(value = "size",required = true,defaultValue = "5") Integer size,
                               @RequestBody(required = false) UserVO userVO){
        Page<User> page = new Page<>(current,size);
        Page<User> userPage = userService.findUserList(page,userVO);
        return Result.ok().data("userPage",userPage);
    }

    /**
     * 功能描述: <br>
     * 〈实现easyExcel的导出功能〉
     * @Param: [response, userVO]
     * @Return: void
     * @Author: Mr.Nie
     * @Date: 2022/3/8 10:22
     */
    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {
        //根据条件查询用户信息
        //查询全部的人数
        Integer count = userService.getBaseMapper().selectCount(null);
        //如果想要导出特定的数据,必须要对查询的代码进行重写
        Page<User> page = new Page<>(1,count);
        Page<User> userList = userService.findUserList(page, userVO);
        List<User> users = userList.getRecords();
        userService.exportUserExcel(response,users);

        //根据条件查询用户信息
        //List<User> users = userService.findUsers(userVO);
        //userService.exportUserExcel(response,users);
    }


    /**
     * 用户登入
     *
     * @param username: 用户名
     * @param password: 密码
     * @return
     */
    @ApiOperation(value = "用户登入", notes = "接收参数用户名和密码,登入成功后,返回JWTToken")
    @PostMapping("/login")
    public Result login(@RequestParam(value = "username",required = true) String username,
                        @RequestParam(value = "password",required = true) String password,
                        @RequestParam(value = "verifyCode",required = true) String verifyCode,
                        HttpServletRequest request) {
        /*System.out.println(verifyCode);
        if (!CaptchaUtil.ver(verifyCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return Result.error().message("验证码错误");
        }*/
        String token=userService.login(username,password);
        //登录日志(SpringAop+反射)
        loginLogService.addLoginLog(request);
        return Result.ok().data("token",token);
    }
}

